package com.nandi.deviceinstall.util;

import android.graphics.Bitmap;
import android.os.Environment;
import android.util.Log;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileUtils {
    /**
     * 保存图片到SD卡
     *
     * @param bm         图片bitmap对象
     * @param floderPath 下载文件保存目录
     * @param fileName   文件名称(不带后缀)
     */
    public static void saveImg(Bitmap bm, String floderPath, String fileName) throws IOException {
        //如果不保存在sd下面下面这几行可以不加
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            Log.e("wl","SD卡异常");
            return;
        }

        File folder = new File(floderPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }

        String savePath = folder.getPath() + File.separator + fileName;
        File file = new File(savePath);
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
        bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
        Log.d("wl",savePath + " 保存成功");
        bos.flush();
        bos.close();
    }
}
